package com.hjj.dataStructures.linkedList;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(new DoubleHeroNode(1, "宋江", "及时雨"));
        doubleLinkedList.add(new DoubleHeroNode(2, "卢俊义", "玉麒麟"));
        doubleLinkedList.add(new DoubleHeroNode(3, "吴用", "智多星"));
        doubleLinkedList.add(new DoubleHeroNode(4, "公孙胜", "入云龙"));
        doubleLinkedList.add(new DoubleHeroNode(5, "关胜", "大刀"));
        doubleLinkedList.add(new DoubleHeroNode(6, "林冲", "豹子头"));
        doubleLinkedList.list();
        System.out.println("======测试删除");
        doubleLinkedList.delete(3);
//        doubleLinkedList.delete(1);
        doubleLinkedList.delete(6);
        doubleLinkedList.delete(0);
        doubleLinkedList.delete(7);
        doubleLinkedList.list();
        System.out.println("======测试更新节点");
        doubleLinkedList.update(new DoubleHeroNode(1, "小宋", "及时雨~~~"));
        doubleLinkedList.list();

    }
}

class DoubleLinkedList {
    private DoubleHeroNode head = new DoubleHeroNode(0, "", "");

    public DoubleHeroNode getHead() {
        return head;
    }

    /**
     * 遍历双向链表
     */
    public void list() {
        if (isEmpty()) return;
        DoubleHeroNode temp = head.getNext();
        while (temp != null) {
            System.out.println(temp);
            temp = temp.getNext();
        }

    }

    /**
     * 添加一个节点都双向链表最后
     *
     * @param doubleHeroNode
     */
    public void add(DoubleHeroNode doubleHeroNode) {
        DoubleHeroNode temp = head;
        while (temp.getNext() != null) {
            temp = temp.getNext();
        }
        temp.setNext(doubleHeroNode);
        doubleHeroNode.setPre(temp);
    }

    /**
     * 修改一个节点的内容，双向链表的修改和单向链表的修改基本一致
     *
     * @param doubleHeroNode
     */
    public void update(DoubleHeroNode doubleHeroNode) {
        if (isEmpty()) return;
        // 找到需要修改的节点，并修改
        DoubleHeroNode temp = head.getNext();
        boolean flag = false;
        while (temp != null) {
            if (temp.getNo() == doubleHeroNode.getNo()) {
                flag = true;
                temp.setName(doubleHeroNode.getName());
                temp.setNickname(doubleHeroNode.getNickname());
                break;
            }
            temp = temp.getNext();
        }
        if (!flag) System.out.println("没有找到需要删除的节点：" + doubleHeroNode.getNo());
    }

    /**
     * 从双向链表中删除节点
     * 1 对于双向链表，可以直接找到要删除的这个节点
     * 2 找到后，自我删除即可
     *
     * @param no
     */
    public void delete(int no) {
        if (isEmpty() || no <= 0) return;
//        DoubleHeroNode temp = head;
//        boolean flag = false;
//        while (temp.getNext() != null) {
//            if (temp.getNext().getNo() == no) {
//                flag = true;
//                if (temp.getNext().getNext() != null)
//                    temp.setNext(temp.getNext().getNext());
//                    temp.getNext().setPre(temp);
//                } else {
//                    temp.setNext(temp.getNext().getNext());
//                }
//                break;
//            }
//            temp = temp.getNext();
//        }


        DoubleHeroNode temp = head;
        boolean flag = false;
        while (temp != null) {
            if (temp.getNo() == no) {
                flag = true;
                break;
            }
            temp = temp.getNext();
        }
        if (flag) {
            temp.getPre().setNext(temp.getNext());
            if (temp.getNext() != null) {
                temp.getNext().setPre(temp.getPre());
            }
        } else {
            System.out.println("没有找到要删除的节点：" + no);
        }

//        while (temp != null) {
//            if (temp.getNo() == no){
//                flag = true;
//                temp.getPre().setNext(temp.getNext());
//                if (temp.getNext() != null){
//                    temp.getNext().setPre(temp.getPre());
//                }
//                break;
//            }
//            temp = temp.getNext();
//        }
//        if (!flag) System.out.println("没有找到要删除的节点：" + no);
    }

    public boolean isEmpty() {
        if (head.getNext() == null) {
            System.out.println("当前链表为空");
            return true;
        } else {
            return false;
        }
    }
}

/**
 * 创建一个双向链表
 */
class DoubleHeroNode {
    private int no;
    private String name;
    private String nickname;
    private DoubleHeroNode next; // 指向下一个节点，默认为null
    private DoubleHeroNode pre; // 指向前一个节点，默认为null

    public DoubleHeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public DoubleHeroNode getNext() {
        return next;
    }

    public void setNext(DoubleHeroNode next) {
        this.next = next;
    }

    public DoubleHeroNode getPre() {
        return pre;
    }

    public void setPre(DoubleHeroNode pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return "DoubleHeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
